import { menus } from "@/config/menu";
import { MenuDataItem } from "@ant-design/pro-components";
import { checkAccess } from "./checkAccess";

/**
 * 根据用户权限获取可访问菜单
 * @param menusList 菜单列表
 * @param loginUser 当前用户
 */
export const getAccessibleMenus = (
  loginUser: API.LoginUserVO,
  menusList: MenuDataItem[] = menus
) => {
  // 当前用户权限，默认未登录
  return menusList.filter((menu) => {
    if (!checkAccess(loginUser, menu.access)) {
      return false;
    }
    if (menu.children) {
      menu.children = getAccessibleMenus(loginUser, menu.children);
    }
    return true;
  });
};
